package com.cy.ppj.comm.config;

import com.cy.ppj.comm.interceptor.AdminLoginInterceptor;
import com.cy.ppj.comm.interceptor.UserLoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.StandardCharsets;

/**
 * @description: Web配置
 * @author: Ocean
 * @create: 2018-07-23 17:05
 **/
@Slf4j
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private UserLoginInterceptor loginInterceptor;
    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;
    @Value("${spring.profiles.active}")
    private String active;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

//        if ("dev".equals(active)) {
            //添加拦截器
            registry.addInterceptor(loginInterceptor)
                    .addPathPatterns("/**")
                    .excludePathPatterns("/", "/wechat/verify/token", "/wechat/share/signature", "/wechat/**/callback", "/wechat/login/url",
                            "/error", "/admin/**", "/home/**", "/swagger-resources", "/swagger-resources/**", "/gateway/**");
            registry.addInterceptor(adminLoginInterceptor)
                    .addPathPatterns("/admin/**")
                    .excludePathPatterns("/admin/login", "/admin/clear/cache");
//            return;
//        }

//        //添加拦截器
//        registry.addInterceptor(loginInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns("/", "/wechat/verify/token", "/wechat/share/signature", "/wechat/**/callback", "/wechat/login/url",
//                        "/error", "/admin/**", "/home/**", "/gateway/**");
//        registry.addInterceptor(adminLoginInterceptor)
//                .addPathPatterns("/admin/**", "/swagger-resources", "/swagger-resources/**")
//                .excludePathPatterns("/admin/login");
    }

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
}
